package com.ruoyi.project.goods.controller;

import cn.mabach.goods.dto.ProductParam;
import cn.mabach.goods.dto.ProductResult;
import cn.mabach.goods.entity.ProductEntity;
import cn.mabach.goods.entity.ProductVertifyRecordEntity;
import cn.mabach.goods.vo.ProductQueryParam;
import cn.mabach.result.PageResult;
import cn.mabach.result.RS;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.goods.feign.ProductServiceFeign;
import com.ruoyi.project.goods.feign.ProductVertifyRecordServiceFeign;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;


/**
 * 商品信息
 *
 * @author chenshun
 * @email 176468159@qq.com
 * @date 2019-12-26 13:45:39
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductServiceFeign productService;
    @Autowired
    private ProductVertifyRecordServiceFeign productVertifyRecordServiceFeign;
    @Autowired
    private TokenService tokenService;



    /**
     * 创建商品
     */
    @PostMapping(value = "/create")
    @PreAuthorize("@ss.hasPermi('pms:product:create')")
    public RS create(@RequestBody ProductParam productParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

        return productService.saveE(productParam);


    }


    /**
     * 信息
     */

    @GetMapping(value = "/updateInfo/{id}")
    @PreAuthorize("@ss.hasPermi('pms:product:read')")
    public RS<ProductResult> getItem(@PathVariable("id") Long id) {
        return productService.getByIdE(id);
    }




    /**
     * 修改
     */
    @PostMapping(value = "/update/{id}")
    @PreAuthorize("@ss.hasPermi('pms:product:update')")
    public RS update(@PathVariable("id") Long id, @RequestBody ProductParam entity) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        entity.setId(id);
        RS rs = productService.updateByIdE(entity);

        return rs;
    }


    /**
     * 分页列表
     */

    @RequestMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('pms:product:read')")
    public RS<PageResult<ProductEntity>> getList(ProductQueryParam productQueryParam,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        RS<PageResult<ProductEntity>> pageResultRS = productService.queryPage(productQueryParam, pageNum, pageSize);

        return pageResultRS;
    }


    //"根据商品名称或货号模糊查询")
//    @RequestMapping(value = "/simpleList", method = RequestMethod.GET)
//    @ResponseBody
//    public RS<List<ProductEntity>> getList(String keyword) {
//        List<ProductEntity> productList = productService.list(keyword);
//        return RS.ok(productList);
//    }


   //"批量修改审核状态")
    @PostMapping(value = "/update/verifyStatus")
    @PreAuthorize("@ss.hasPermi('pms:product:update')")
    @Transactional
    public RS updateVerifyStatus(@RequestParam("ids") List<Long> ids,
                                 @RequestParam("verifyStatus") Integer verifyStatus,
                                 @RequestParam("detail") String detail, HttpServletRequest request) {
        Boolean flag=false;

        for (Long id : ids) {
            productService.updateVerifyStatus(id,verifyStatus);
            ProductVertifyRecordEntity recordEntity = new ProductVertifyRecordEntity();
            recordEntity.setProductId(id);
            recordEntity.setCreateTime(new Date());
            recordEntity.setStatus(verifyStatus);
            recordEntity.setDetail(detail);
//            获取管理员用户名
            String username = tokenService.getLoginUser(request).getUsername();
            recordEntity.setVertifyMan(username);
            productVertifyRecordServiceFeign.saveE(recordEntity);

        }

        flag=true;
        if (flag){
            return RS.ok();
        }

       return RS.error();
    }

    /**
     * 批量上下架
     */
    @PostMapping(value = "/update/publishStatus")
    @PreAuthorize("@ss.hasPermi('pms:product:update')")
    public RS updatePublishStatus(@RequestParam("ids") List<Long> ids,
                                  @RequestParam("publishStatus") Integer publishStatus) {
       return productService.updatePublishStatus(ids,publishStatus);
    }


    /**
     * 批量推荐商品
     */
    @PostMapping(value = "/update/recommendStatus")
    @PreAuthorize("@ss.hasPermi('pms:product:update')")
    public RS updateRecommendStatus(@RequestParam("ids") List<Long> ids,
                                              @RequestParam("recommendStatus") Integer recommendStatus) {
       return productService.updateRecommendStatus(ids,recommendStatus);
    }


    /**
     * 批量设为新品
     */

    @PostMapping(value = "/update/newStatus")
    @PreAuthorize("@ss.hasPermi('pms:product:update')")
    public RS updateNewStatus(@RequestParam("ids") List<Long> ids,
                              @RequestParam("newStatus") Integer newStatus) {
        return productService.updateNewStatus(ids,newStatus);

    }

    /**
     * 批量逻辑删除
     */
    @PostMapping(value = "/update/deleteStatus")
    @PreAuthorize("@ss.hasPermi('pms:product:delete')")
    public RS updateDeleteStatus(@RequestParam("ids") List<Long> ids,
                                 @RequestParam("deleteStatus") Integer deleteStatus) {

        return productService.updateDeleteStatus(ids,deleteStatus);
    }






}
